package com.apes.fn.business.fixedLoss.modle;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.plugin.soe.metadata.BusinessAttributeValue;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName BusinessStrategy
 * @Author wsj
 * @Date 2021/5/7 14:21
 * @description 商务业务策略模型
 */
@Entity
@Table(name =BusinessStrategy.CONST_TABLE_NAME )
@SelectBeforeUpdate
@Getter
@Setter
@DynamicInsert
@DynamicUpdate
@EntityListeners(DefaultPersistListener.class)
public class BusinessStrategy extends Model {
    public final static String CONST_TABLE_NAME = "Business_Strategy";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "商务业务策略", padding = 5, prefix = "SW%(yy)%(month)", useDateRange = true)
    @Comment("主键编码")
    private  String id;

    @States(name = "业务策略类型", values = {
            @State(id = "A", name = "定损特权额度"),
            @State(id = "B", name = "车险销售任务"),
            @State(id = "C", name = "商业险销售任务")
    })
    @Comment("业务策略类型")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "strategy_Type", referencedColumnName = "id")
    private BusinessAttributeValue strategyType;

    @Column(name = "name",nullable = false,length = 128)
    @Comment("策略名称")
    private  String name;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "valid_Period_Start")
    @Comment("有效期起")
    private Date validPeriodStart;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "valid_Period_End")
    @Comment("有效期止")
    private Date validPeriodEnd;

    @Column(name = "state")
    @Comment("状态值")
    private  String state ="create";


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "operation", referencedColumnName = "id")
    @Comment("运营模式")
    private Enumeration operation;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    @Comment("审核人")
    private PartyRole approveUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "approve_Date")
    @Comment("审核日期")
    private Date approveDate;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "delete_uid", referencedColumnName = "id")
    @Comment("删除人")
    private PartyRole deleteUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "delete_date")
    @Comment("删除日期")
    private Date deleteDate;

    @OneToMany(mappedBy = "businessStrategy", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<BusinessStrategyItem> businessStrategyItems = new ArrayList<>();

    public List<BusinessStrategyItem> getBusinessStrategyItems() {
        for(BusinessStrategyItem item : businessStrategyItems){
            item.setBusinessStrategy(this);
        }
        return businessStrategyItems;
    }

    public void  checkValidPeriod() {
        if (validPeriodStart.after(validPeriodEnd)){
            throw new RuntimeException("有效期止不能在有效期起之前!!!");
        }
    }
}
